package com.jeeplus.test.jlnu.student.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.jeeplus.test.jlnu.student.domain.entity.*;
import com.jeeplus.test.jlnu.student.dto.VerifyResponse;
import com.jeeplus.test.jlnu.student.mapper.BsssStudentFamilyMapper;
import com.jeeplus.test.jlnu.student.mapper.StudentInfoApproveMapper;
import com.jeeplus.test.jlnu.student.service.BsssStudentFamilyService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BsssStudentFamilyServiceImpl extends ServiceImpl<BsssStudentFamilyMapper, BsssStudentFamily> implements BsssStudentFamilyService {
    @Autowired
    private  BsssStudentFamilyMapper bsssStudentFamilyMapper;

    @Override
    public BsssStudentFamily getBsssStudentFamily(String studentId, String flag) {
        LambdaQueryChainWrapper<BsssStudentFamily> wrapper = new LambdaQueryChainWrapper<>(bsssStudentFamilyMapper);
        wrapper.eq(BsssStudentFamily::getStudentId,studentId);
        wrapper.eq(BsssStudentFamily::getFlag,flag);
        return wrapper.one();
    }

    @Override
    public Boolean bkbsssStudentObtainUpdate(BsssStudentFamily bsssStudentFamily) {
        LambdaUpdateChainWrapper<BsssStudentFamily> wrapper = new LambdaUpdateChainWrapper<>(bsssStudentFamilyMapper);
        if (ObjectUtil.isNotEmpty(bsssStudentFamily.getFamilyMember1Name())){
            wrapper.set(BsssStudentFamily::getFamilyMember1Name,bsssStudentFamily.getFamilyMember1Name());
        }
        if (ObjectUtil.isNotEmpty(bsssStudentFamily.getFamilyMember1WorkUnit())){
            wrapper.set(BsssStudentFamily::getFamilyMember1WorkUnit,bsssStudentFamily.getFamilyMember1WorkUnit());
        }
        if (ObjectUtil.isNotEmpty(bsssStudentFamily.getFamilyMember1Position())){
            wrapper.set(BsssStudentFamily::getFamilyMember1Position,bsssStudentFamily.getFamilyMember1Position());
        }
        if (ObjectUtil.isNotEmpty(bsssStudentFamily.getFamilyMember1Phone())){
            wrapper.set(BsssStudentFamily::getFamilyMember1Phone,bsssStudentFamily.getFamilyMember1Phone());
        }
        if (ObjectUtil.isNotEmpty(bsssStudentFamily.getFamilyMember1Residence())){
            wrapper.set(BsssStudentFamily::getFamilyMember1Residence,bsssStudentFamily.getFamilyMember1Residence());
        }
        if (ObjectUtil.isNotEmpty(bsssStudentFamily.getFamilyMember1PoliticalStatus())){
            wrapper.set(BsssStudentFamily::getFamilyMember1PoliticalStatus,bsssStudentFamily.getFamilyMember1PoliticalStatus());
        }
        if (ObjectUtil.isNotEmpty(bsssStudentFamily.getFamilyMember1Relationship())){
            wrapper.set(BsssStudentFamily::getFamilyMember1Relationship,bsssStudentFamily.getFamilyMember1Relationship());
        }


        if (ObjectUtil.isNotEmpty(bsssStudentFamily.getFamilyMember2Name())){
            wrapper.set(BsssStudentFamily::getFamilyMember2Name,bsssStudentFamily.getFamilyMember2Name());
        }
        if (ObjectUtil.isNotEmpty(bsssStudentFamily.getFamilyMember2Relationship())){
            wrapper.set(BsssStudentFamily::getFamilyMember2Relationship,bsssStudentFamily.getFamilyMember2Relationship());
        }
        if (ObjectUtil.isNotEmpty(bsssStudentFamily.getFamilyMember2WorkUnit())){
            wrapper.set(BsssStudentFamily::getFamilyMember2WorkUnit,bsssStudentFamily.getFamilyMember2WorkUnit());
        }
        if (ObjectUtil.isNotEmpty(bsssStudentFamily.getFamilyMember2Position())){
            wrapper.set(BsssStudentFamily::getFamilyMember2Position,bsssStudentFamily.getFamilyMember2Position());
        }
        if (ObjectUtil.isNotEmpty(bsssStudentFamily.getFamilyMember2Phone())){
            wrapper.set(BsssStudentFamily::getFamilyMember2Phone,bsssStudentFamily.getFamilyMember2Phone());
        }
        if (ObjectUtil.isNotEmpty(bsssStudentFamily.getFamilyMember2Residence())){
            wrapper.set(BsssStudentFamily::getFamilyMember2Residence,bsssStudentFamily.getFamilyMember2Residence());
        }
        if (ObjectUtil.isNotEmpty(bsssStudentFamily.getFamilyMember2PoliticalStatus())){
            wrapper.set(BsssStudentFamily::getFamilyMember2PoliticalStatus,bsssStudentFamily.getFamilyMember2PoliticalStatus());
        }
        if (ObjectUtil.isNotEmpty(bsssStudentFamily.getFamilyMember2Relationship())){
            wrapper.set(BsssStudentFamily::getFamilyMember2Relationship,bsssStudentFamily.getFamilyMember2Relationship());
        }

        if (ObjectUtil.isNotEmpty(bsssStudentFamily.getFamilyMember3Name())){
            wrapper.set(BsssStudentFamily::getFamilyMember3Name,bsssStudentFamily.getFamilyMember3Name());
        }
        if (ObjectUtil.isNotEmpty(bsssStudentFamily.getFamilyMember3Relationship())){
            wrapper.set(BsssStudentFamily::getFamilyMember3Relationship,bsssStudentFamily.getFamilyMember3Relationship());
        }
        if (ObjectUtil.isNotEmpty(bsssStudentFamily.getFamilyMember3WorkUnit())){
            wrapper.set(BsssStudentFamily::getFamilyMember3WorkUnit,bsssStudentFamily.getFamilyMember3WorkUnit());
        }
        if (ObjectUtil.isNotEmpty(bsssStudentFamily.getFamilyMember3Position())){
            wrapper.set(BsssStudentFamily::getFamilyMember3Position,bsssStudentFamily.getFamilyMember3Position());
        }
        if (ObjectUtil.isNotEmpty(bsssStudentFamily.getFamilyMember3Phone())){
            wrapper.set(BsssStudentFamily::getFamilyMember3Phone,bsssStudentFamily.getFamilyMember3Phone());
        }
        if (ObjectUtil.isNotEmpty(bsssStudentFamily.getFamilyMember3Residence())){
            wrapper.set(BsssStudentFamily::getFamilyMember3Residence,bsssStudentFamily.getFamilyMember3Residence());
        }
        if (ObjectUtil.isNotEmpty(bsssStudentFamily.getFamilyMember3PoliticalStatus())){
            wrapper.set(BsssStudentFamily::getFamilyMember3PoliticalStatus,bsssStudentFamily.getFamilyMember3PoliticalStatus());
        }
        if (ObjectUtil.isNotEmpty(bsssStudentFamily.getFamilyMember3Relationship())){
            wrapper.set(BsssStudentFamily::getFamilyMember3Relationship,bsssStudentFamily.getFamilyMember3Relationship());
        }

        if (ObjectUtil.isNotEmpty(bsssStudentFamily.getFamilyMember4Name())){
            wrapper.set(BsssStudentFamily::getFamilyMember4Name,bsssStudentFamily.getFamilyMember4Name());
        }
        if (ObjectUtil.isNotEmpty(bsssStudentFamily.getFamilyMember4Relationship())){
            wrapper.set(BsssStudentFamily::getFamilyMember4Relationship,bsssStudentFamily.getFamilyMember4Relationship());
        }
        if (ObjectUtil.isNotEmpty(bsssStudentFamily.getFamilyMember4WorkUnit())){
            wrapper.set(BsssStudentFamily::getFamilyMember4WorkUnit,bsssStudentFamily.getFamilyMember4WorkUnit());
        }
        if (ObjectUtil.isNotEmpty(bsssStudentFamily.getFamilyMember4Position())){
            wrapper.set(BsssStudentFamily::getFamilyMember4Position,bsssStudentFamily.getFamilyMember4Position());
        }
        if (ObjectUtil.isNotEmpty(bsssStudentFamily.getFamilyMember4Phone())){
            wrapper.set(BsssStudentFamily::getFamilyMember4Phone,bsssStudentFamily.getFamilyMember4Phone());
        }
        if (ObjectUtil.isNotEmpty(bsssStudentFamily.getFamilyMember4Residence())){
            wrapper.set(BsssStudentFamily::getFamilyMember4Residence,bsssStudentFamily.getFamilyMember4Residence());
        }
        if (ObjectUtil.isNotEmpty(bsssStudentFamily.getFamilyMember4PoliticalStatus())){
            wrapper.set(BsssStudentFamily::getFamilyMember4PoliticalStatus,bsssStudentFamily.getFamilyMember4PoliticalStatus());
        }
        if (ObjectUtil.isNotEmpty(bsssStudentFamily.getFamilyMember4Relationship())){
            wrapper.set(BsssStudentFamily::getFamilyMember4Relationship,bsssStudentFamily.getFamilyMember4Relationship());
        }

        if (ObjectUtil.isNotEmpty(bsssStudentFamily.getFamilyMember5Name())){
            wrapper.set(BsssStudentFamily::getFamilyMember5Name,bsssStudentFamily.getFamilyMember5Name());
        }
        if (ObjectUtil.isNotEmpty(bsssStudentFamily.getFamilyMember5Relationship())){
            wrapper.set(BsssStudentFamily::getFamilyMember5Relationship,bsssStudentFamily.getFamilyMember5Relationship());
        }
        if (ObjectUtil.isNotEmpty(bsssStudentFamily.getFamilyMember5WorkUnit())){
            wrapper.set(BsssStudentFamily::getFamilyMember5WorkUnit,bsssStudentFamily.getFamilyMember5WorkUnit());
        }
        if (ObjectUtil.isNotEmpty(bsssStudentFamily.getFamilyMember5Position())){
            wrapper.set(BsssStudentFamily::getFamilyMember5Position,bsssStudentFamily.getFamilyMember5Position());
        }
        if (ObjectUtil.isNotEmpty(bsssStudentFamily.getFamilyMember5Phone())){
            wrapper.set(BsssStudentFamily::getFamilyMember5Phone,bsssStudentFamily.getFamilyMember5Phone());
        }
        if (ObjectUtil.isNotEmpty(bsssStudentFamily.getFamilyMember5Residence())){
            wrapper.set(BsssStudentFamily::getFamilyMember5Residence,bsssStudentFamily.getFamilyMember5Residence());
        }
        if (ObjectUtil.isNotEmpty(bsssStudentFamily.getFamilyMember5PoliticalStatus())){
            wrapper.set(BsssStudentFamily::getFamilyMember5PoliticalStatus,bsssStudentFamily.getFamilyMember5PoliticalStatus());
        }
        if (ObjectUtil.isNotEmpty(bsssStudentFamily.getFamilyMember5Relationship())){
            wrapper.set(BsssStudentFamily::getFamilyMember5Relationship,bsssStudentFamily.getFamilyMember5Relationship());
        }
        //todo 后期新增学籍变更信息 字段 ↓

        //根据id修改
        wrapper.eq(BsssStudentFamily::getId, bsssStudentFamily.getId());
        return wrapper.update();
    }

    @Override
    public int saveStudentFamily(BsssStudentFamily bsssStudentFamily) {

        LambdaQueryChainWrapper<BsssStudentFamily> wrapper =
                new LambdaQueryChainWrapper<>(bsssStudentFamilyMapper);

        // 查询是否存在记录
        List<BsssStudentFamily> dataList = wrapper
                .eq(BsssStudentFamily::getStudentId, bsssStudentFamily.getStudentId())
                .list();

        if(dataList.size() > 3){
            throw new RuntimeException("只允许添加三位家庭成员");
        }

        //查询基本信息审查表中是否存在修改表
        // 改用查询条件构造器
//        LambdaQueryChainWrapper<StudentInfoApprove> queryWrapper =
//                new LambdaQueryChainWrapper<>(studentInfoApproveMapper);
//
//        // 查询是否存在记录
//        Optional<StudentInfoApprove> info = queryWrapper
//                .eq(StudentInfoApprove::getStudentId, bsssStudentFamily.getStudentId())
//                .eq(StudentInfoApprove::getOutsideType, "1")
//                .oneOpt();
//        // 生成 VerifyResponse 的 JSON 数据
//        String data = generateVerifyResponseJson(bsssStudentFamily);
//
//
//        // 根据查询结果执行更新或插入
//        if (info.isPresent()) {
//            // 更新操作
//            StudentInfoApprove studentInfoApprove = new StudentInfoApprove();
//            studentInfoApprove.setContent(data);
//            studentInfoApprove.setStudentId(bsssStudentFamily.getStudentId());
//            studentInfoApprove.setOutsideType("1");
//            studentInfoApproveMapper.updateStudentInfoApprove(studentInfoApprove);
//        } else {
//            // 插入操作
//            StudentInfoApprove studentInfoApprove = createNewStudentInfoApprove(data,bsssStudentFamily);
//            studentInfoApproveMapper.insertStudentInfoApprove(studentInfoApprove);
//        }

        // 插入操作
        return bsssStudentFamilyMapper.insert(bsssStudentFamily);
    }

    @Override
    public int deleteStudentFamily(List<Long> ids) {
        return bsssStudentFamilyMapper.deleteBatchIds(ids);
    }

    @Override
    public int updateStudentFamily(BsssStudentFamily bsssStudentFamily) {
        return bsssStudentFamilyMapper.updateById(bsssStudentFamily);
    }

    @Override
    public List<BsssStudentFamily> getStudentFamily(String studentId) {
        List<BsssStudentFamily> result = new LambdaQueryChainWrapper<>(bsssStudentFamilyMapper)
                .eq(BsssStudentFamily::getStudentId, studentId)
                .list();
        return result;
    }

    // 生成 VerifyResponse 的 JSON 数据
    private String generateVerifyResponseJson(BsssStudentFamily bsssStudentFamily) {
        try {

            VerifyResponse verifyResponse = new VerifyResponse();
            BeanUtils.copyProperties(bsssStudentFamily, verifyResponse);

            // JSON 序列化
            ObjectMapper mapper = new ObjectMapper();
            return mapper.writeValueAsString(verifyResponse);
        } catch (JsonProcessingException e) {
            throw new RuntimeException("JSON 序列化失败", e);
        }
    }

    // 创建新记录对象
    private StudentInfoApprove createNewStudentInfoApprove(String data,BsssStudentFamily bsssStudentFamily) {
        StudentInfoApprove studentInfoApprove = new StudentInfoApprove();
        studentInfoApprove.setStudentId(bsssStudentFamily.getStudentId());
        studentInfoApprove.setContent(data);
        studentInfoApprove.setOfficeId(bsssStudentFamily.getOfficeId());
        studentInfoApprove.setOutsideType("1");
        studentInfoApprove.setApprovalStatus(0);
        return studentInfoApprove;
    }
}
